import  cv2
import numpy as np
if __name__ == '__main__':
    image_np = cv2.imread('work2.jpg')

    image_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
    _, image_thresh = cv2.threshold(image_gray, 127, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
    countours, hierarchy = cv2.findContours(image_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in countours:
        # print(cnt.shape)
        x, y, z = map(int, np.random.randint(0, 256, 3))
        rect = cv2.minAreaRect(cnt)
        points = cv2.boxPoints(rect)

        points = np.int32(points)
        print(points)
        cv2.drawContours(image_np, [points],-1, (x, y, z), 2 )




    cv2.imshow('', image_np)
    cv2.imwrite('zuoye2.jpg', image_np)
    cv2.waitKey(0)